package cn.sm1234.quartz;

import org.quartz.spi.JobFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

@Configuration
public class QuartzConfig {

    // 采用Apdapterr createJobInstance。 MyJob不是spring环境
    // JOB
    @Bean
    public JobDetailFactoryBean getJobDetailFactoryBean(){
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();

        factoryBean.setJobClass(MyJob.class);
        return  factoryBean;
    }

    // TRIGGER

//    @Bean
//    public SimpleTriggerFactoryBean getSimpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
//        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
//        factoryBean.setJobDetail(jobDetailFactoryBean.getObject());
//
//        // 重复时间， 毫秒
//        factoryBean.setRepeatInterval(5000);
//
//        factoryBean.setRepeatCount(4);
//        return factoryBean;
//    }

    @Bean
    public CronTriggerFactoryBean getCronTriggerFactoryBean (JobDetailFactoryBean jobDetailFactoryBean){
        CronTriggerFactoryBean  factoryBean = new CronTriggerFactoryBean ();
        factoryBean.setJobDetail(jobDetailFactoryBean.getObject());

        // 重复时间， 毫秒
        factoryBean.setCronExpression("0/4 * * * * ?");
        return factoryBean;
    }

    // SCHEDULER
    @Bean
    public SchedulerFactoryBean getSchedulerFactoryBean(CronTriggerFactoryBean factoryBean, JobFactory jobFactory){
        SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
        scheduler.setTriggers(factoryBean.getObject());

        // 重写jobfactory
        scheduler.setJobFactory(jobFactory);
        return scheduler;
    }
}
